#ifndef WRAP_ERROR_H
#define WRAP_ERROR_H

#include <cstdio>

namespace Wrap {

template<typename T>
inline T perror_neg(T &&t, const char *func, const char *file, int line) {
    if (t < 0) {
        ::perror(func);
        ::fprintf(stderr, "result<0 in %s:%d\n", file, line);
    }
    return t;
}

template<typename T>
inline T perror_eq0(T &&t, const char *func, const char *file, int line) {
    if (!t) {
        ::perror(func);
        ::fprintf(stderr, "result=0 in %s:%d\n", file, line);
    }
    return t;
}

template<typename T>
inline T perror_neq0(T &&t, const char *func, const char *file, int line) {
    if (t) {
        ::perror(func);
        ::fprintf(stderr, "result!=0 in %s:%d\n", file, line);
    }
    return t;
}


/// @see https://man7.org/linux/man-pages/man3/strerror_r.3.html
/// @return the GNU-specific strerror_r() functions return the appropriate
/// error description string, or an "Unknown error nnn" message if the error
/// number is unknown.
/// @throw EINVAL The value of @c errnum is not a valid error number.
/// @throw ERANGE Insufficient storage was supplied to contain the error
/// description string.
char *strerror_r_(int errnum);
}

#define PERROR_NEG(x) (Wrap::perror_neg((x), __func__, __FILE__, __LINE__))
#define PERROR_EQ0(x) (Wrap::perror_eq0((x), __func__, __FILE__, __LINE__))
#define PERROR_NEQ0(x) (Wrap::perror_neq0((x), __func__, __FILE__, __LINE__))


#endif